﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bootstrap.MVC.Common;
using Bootstrap.MVC.Infrastructure.Html;
using Bootstrap.MVC.Utils;

namespace Bootstrap.MVC.Widgets
{
    public class ButtonDropDownHtmlGenerator : HtmlGeneratorBindableBase<IButtonDropDownModel>
    {
        public ButtonDropDownHtmlGenerator(IButtonDropDownModel model) : base(model) { }



        //<div class="btn-group">
        //    <button class="btn dropdown-toggle" data-toggle="dropdown">Action <span class="caret"></span></button>
        //    <ul class="dropdown-menu">
        //        <li><a href="#">Action</a></li>
        //        <li><a href="#">Another action</a></li>
        //        <li><a href="#">Something else here</a></li>
        //        <li class="divider"></li>
        //        <li><a href="#">Separated link</a></li>
        //    </ul>
        //</div>

        public override HtmlTagBuilder AsHtmlBuilder()
        {
            var containerTag = new HtmlTagBuilder("div")
                                   .Attr(new { @class = "btn-group" });

            if (Model.DropUp)
                containerTag.AddClass("dropup");

            var btnTag = new HtmlTagBuilder("button").Attr("class", "btn").Text(Model.Text);

            if (Model.Skin != Skins.Default)
                btnTag.AddClass("btn-" + Model.Skin.GetStringValue());

            if (Model.Disabled)
                btnTag.AddClass("disabled");

            containerTag.Append(btnTag);

            if(Model.Split)
            {
                var splitTag = new HtmlTagBuilder("button").Attr("class", "btn");
                StyleSplitButton(splitTag);

                if (Model.Size != ButtonSize.Normal)
                    splitTag.AddClass(Model.Size.GetStringValue());

                if (Model.Skin != Skins.Default)
                    splitTag.AddClass("btn-" + Model.Skin.GetStringValue());

                if (Model.Disabled)
                    splitTag.AddClass("disabled");
                containerTag.Append(splitTag);
            }
            else
            {
                StyleSplitButton(btnTag);
            }


            if (Model.Size != ButtonSize.Normal)
                btnTag.AddClass(Model.Size.GetStringValue());

            //var dropdownTag = new HtmlTagBuilder("ul").Attr("class", "dropdown-menu");
            //foreach (var item in Model.Items)
            //{
            //    dropdownTag.Append(new DropDownItemHtmlGenerator(item).AsHtmlBuilder());
            //}
            var dropdownTag = new DropDownMenuHtmlGenerator(Model.Menu).AsHtmlBuilder();
            containerTag.Append(dropdownTag);

            return containerTag;
        }

        public override string Render()
        {
            return AsHtmlBuilder().Render();
        }


        private void StyleSplitButton(HtmlTagBuilder tag)
        {
            tag.AddClass("dropdown-toggle")
                               .Attr("data-toggle", "dropdown")
                               .Append(new HtmlTagBuilder("span").Attr("class", "caret"));
        }
        private HtmlTagBuilder createMenuItem(IDropDownItemModel item)
        {
            var liTag = new HtmlTagBuilder("li");
             
            var aTag = new HtmlTagBuilder("a").Attr("href", item.Url).Text( item.Text);
            foreach(var e in item.Events.EventBag)
            {
                aTag.Attr(e.EventName , "{0}()".FormatWith(e.Handler.Replace("()","")));
            }

            return liTag.Append(aTag);
        }

    }
}
